package com.qcxk.yuqian.rtu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.qcxk.yuqian.rtu.controller.model.response.Response;
import com.qcxk.yuqian.rtu.entity.RtuOriginalData;
import com.qcxk.yuqian.rtu.mapper.RtuDataMapper;
import com.qcxk.yuqian.rtu.mapper.RtuHisDataMapper;
import com.qcxk.yuqian.rtu.service.IRtuDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcxk.yuqian.rtu.utils.RestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @auther lp
 * @create 2021-11-15 03:23:05
 * @describe 服务实现类
 */
@Service
public class RtuDataServiceImpl extends ServiceImpl<RtuDataMapper, RtuOriginalData> implements IRtuDataService {
    @Autowired
    private RtuDataMapper rtuDataMapper;
    @Autowired
    private RtuHisDataMapper rtuHisDataMapper;

    @Override
    public Page queryRtuDataPage(Page page, RtuOriginalData rtuOriginalData, String startTime, String endTime) {

        QueryWrapper<RtuOriginalData> rtuDataQueryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(rtuOriginalData.getRtuNo())) {
            rtuDataQueryWrapper.like("rtu_no", rtuOriginalData.getRtuNo().toUpperCase());
        }

        if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
            rtuDataQueryWrapper.between("ctime", startTime, endTime);
        }
        rtuDataQueryWrapper.orderByDesc("ctime");
        rtuDataMapper.selectPage(page, rtuDataQueryWrapper);
        return page;
    }

    @Override
    public boolean deleteRtuDataById(RtuOriginalData rtuOriginalData) {
        int re = rtuHisDataMapper.deleteById(rtuOriginalData.getId());
        return re == 1 ? true : false;
    }

    @Override
    public Response getRealTimeData(String rtuNo) {
        Response result = RestUtils.restPostFortoken("http://192.168.22.30:9999/receive/get",rtuNo,null, Response.class);
        return result;
    }
}
